<%= form_for(post, url: '/pages') do |f| %>
    <%= render 'shared/error_messages', object: post %>

    <div class="field">
      <%= f.label :title %>
      <%= f.text_field :title %>
    </div>

    <div class="field">
      <%= f.label :context %>
      <%= f.text_area :context, :style => 'width; 100px' %>
    </div>
    <span class="picture">
      <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
    </span>
    <image src="" id="image"></image>
    <%= f.hidden_field :image, id:'image_url' %>
    <span class="picture_url">
      <%= f.file_field :image, name: 'image_path', id: 'current_image_url', accept: 'image/jpeg,image/gif,image/png' %>
    </span>

    <%= f.hidden_field :attachment %>
    <span>
      <%= f.file_field :attachment, name: 'attachment_path', id: 'attachment', accept: 'file/zip' %>
    </span>

    <div class="actions">
      <%= link_to t(:back), posts_path %>
      <%= f.submit value: t(:post) %>
    </div>

<% end %>


<script type="text/javascript">
    $('#post_picture').bind('change', function () {
        var size_in_megabytes = this.files[0].size / 1024 / 1024;
        if (size_in_megabytes > 5) {
            alert('Maximum file size is 5MB. Please choose a smaller file.');
        }
    });


    $('#current_image_url').bind('change', function () {
        var size_in_megabytes = this.files[0].size / 1024 / 1024;
        if (size_in_megabytes > 5) {
            alert('Maximum file size is 5MB. Please choose a smaller file.');
        }

    });

    $("#image").bind('click', function(){
        $('#image').removeAttr("src");
        $('image_url').val('');
    })

    /**
     * 账户和美食的有关图片上传的js文件，千万别误删了！！！！
     */
// (function($, undefined) {
    $(function () {
        //文件上传
        function fileUploadStart() {
            $('#attachment').uploadifySettings('scriptData', {
                'imgHeight': 128,
                'imgWidth': 128,
                'fixName': '*.zip',
                'imgType': 1
            });
        }

        function fileUploadSuccess(event, queueID, fileObj, response) {
            var res = JSON.parse(response);
            $('#post_attachment').val(res.image_url)
            $('#post_attachment').val(res.image_url)
            console.log(res)
        }

        $(function () {
            $.mitian.initSingleFileUploader2('#attachment',
                    "*.zip", 1024 * 1024, fileUploadStart,
                    fileUploadSuccess);
        });


        function uploadStart() {
            $('#current_image_url').uploadifySettings('scriptData', {
                'imgHeight' : 128,
                'imgWidth' : 128,
                'fixName' : '*.jpeg;*.jpg;*.png',
                'imgType' : 1
            });
        }


        function uploadSuccess(event, queueID, fileObj, response) {
            var res = JSON.parse(response);
            $('#image').attr("src", res.image_url);
            $('#image_url').val(res.image_url)
            console.log(res)
        }

        $(function() {

            $.mitian.initSingleFileUploader('#current_image_url',
                    "*.jpeg;*.jpg;*.png", 1024 * 1024, uploadStart,
                    uploadSuccess);


        });
    })





</script>
